<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="JkMemberInfoForm">
          <a-row>

						<a-col :span="24">
							<a-form-item label="会员编号"     v-bind="validateInfos.username" id="JkMemberInfoForm-username" name="username">
								<a-input v-model:value="formData.username" placeholder="请输入会员编号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="用户姓名" v-bind="validateInfos.realname" id="JkMemberInfoForm-realname" name="realname">
								<a-input v-model:value="formData.realname" placeholder="请输入用户姓名"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="昵称" v-bind="validateInfos.nickname" id="JkMemberInfoForm-nickname" name="nickname">
								<a-input v-model:value="formData.nickname" placeholder="请输入昵称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="性别" v-bind="validateInfos.gender" id="JkMemberInfoForm-gender" name="gender">
								<j-dict-select-tag v-model:value="formData.gender" dictCode="sex" placeholder="请选择性别"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="生日" v-bind="validateInfos.birthday" id="JkMemberInfoForm-birthday" name="birthday">
								<a-date-picker placeholder="请选择生日"  v-model:value="formData.birthday" value-format="YYYY-MM-DD"  style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
            <a-col :span="24">
              <a-form-item label="手机号" v-bind="validateInfos.phone" id="JkMemberInfoForm-phone" name="phone">
                <a-input v-model:value="formData.phone" placeholder="请输入手机号"  allow-clear ></a-input>
              </a-form-item>
            </a-col>
						<a-col :span="24">
							<a-form-item label="头像URL" v-bind="validateInfos.avatarUrl" id="JkMemberInfoForm-avatarUrl" name="avatarUrl">
								<j-image-upload :fileMax="1" v-model:value="formData.avatarUrl" ></j-image-upload>
							</a-form-item>
						</a-col>

						<a-col :span="24">
							<a-form-item label="邮箱" v-bind="validateInfos.email" id="JkMemberInfoForm-email" name="email">
								<a-input v-model:value="formData.email" placeholder="请输入邮箱"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
            <a-col :span="24">
              <a-form-item label="所属省" v-bind="validateInfos.province" id="JkMemberInfoForm-province" name="province">
                <j-area-linkage v-model:value="formData.province" placeholder="请输入所属省" saveCode="region"  allow-clear />
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="积分" v-bind="validateInfos.memberScore" id="JkMemberInfoForm-memberScore" name="memberScore">
                <a-input-number v-model:value="formData.memberScore" placeholder="请输入积分" style="width: 100%" />
              </a-form-item>
            </a-col>
            <!--:label-col="{ span: 3 }" :wrapperCol="{ span: 21 }"-->
						<a-col :span="24">
							<a-form-item label="地址"   v-bind="validateInfos.address" id="JkMemberInfoForm-address" name="address">
								<a-input v-model:value="formData.address"    placeholder="请输入地址"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="工作单位"   v-bind="validateInfos.workunit" id="JkMemberInfoForm-workunit" name="workunit">
								<a-input v-model:value="formData.workunit" placeholder="请输入工作单位"  allow-clear ></a-input>
							</a-form-item>
						</a-col>

						<a-col :span="24">
							<a-form-item label="简介"    v-bind="validateInfos.memberDesc" id="JkMemberInfoForm-memberDesc" name="memberDesc">
								<a-textarea v-model:value="formData.memberDesc" :rows="4" placeholder="请输入简介" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="偏好"     v-bind="validateInfos.preference" id="JkMemberInfoForm-preference" name="preference">
								<a-textarea v-model:value="formData.preference" :rows="4" placeholder="请输入偏好" />
							</a-form-item>
						</a-col>

						<a-col :span="24">
							<a-form-item label="会员类别" v-bind="validateInfos.memberType" id="JkMemberInfoForm-memberType" name="memberType">
								<j-dict-select-tag v-model:value="formData.memberType" dictCode="member_type" placeholder="请选择会员类别"  allow-clear />
							</a-form-item>
						</a-col>
            <a-col :span="24">
              <a-form-item label="启用状态" v-bind="validateInfos.isEnabled" id="JkMemberInfoForm-isEnabled" name="isEnabled">
                <j-switch v-model:value="formData.isEnabled"  ></j-switch>
              </a-form-item>
            </a-col>
						<a-col :span="24">
							<a-form-item label="介绍人" v-bind="validateInfos.introducer" id="JkMemberInfoForm-introducer" name="introducer">
								<a-input v-model:value="formData.introducer" placeholder="请输入介绍人"  disabled ></a-input>
							</a-form-item>
						</a-col>

						<a-col :span="24">
							<a-form-item label="注册时间" v-bind="validateInfos.registerTime" id="JkMemberInfoForm-registerTime" name="registerTime">
								<a-date-picker placeholder="请选择注册时间"  v-model:value="formData.registerTime"  disabled  showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSwitch from '/@/components/Form/src/jeecg/components/JSwitch.vue';
  import JAreaLinkage from '/@/components/Form/src/jeecg/components/JAreaLinkage.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../JkMemberInfo.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    username: '',
    realname: '',
    nickname: '',
    gender: '',
    birthday: '',
    avatarUrl: '',
    phone: '',
    email: '',
    address: '',
    workunit: '',
    province: '',
    memberDesc: '',
    preference: '',
    memberScore: undefined,
    memberType: '',
    introducer: '',
    isEnabled: undefined,
    registerTime: '',
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    username: [{ required: true, message: '请输入会员编号!'},],
    realname: [{ required: true, message: '请输入用户姓名!'},],
    phone: [{ required: true, message: '请输入手机号!'},],
    memberType: [{ required: true, message: '请输入会员类别!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });


  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
